<?php

namespace app\admin\model;

use think\Model;
use app\admin\model\Admin as AdminModel;
use app\admin\model\Material as MaterialModel;
use app\admin\model\Country as CountryModel;

class Order extends Model
{





    // 表名
    protected $name = 'order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'whatsup_status_text',
        'admin_whatsup_status_text'
    ];


    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }


    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }

    public function getWhatsupStatusList()
    {
        return ['0' => __('Whatsup_status 0'), '1' => __('Whatsup_status 1'), '2' => __('Whatsup_status 2')];
    }

    public function getAdminWhatsupStatusList()
    {
        return ['0' => __('Admin_whatsup_status 0'), '1' => __('Admin_whatsup_status 1'), '2' => __('Admin_whatsup_status 2')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getWhatsupStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['whatsup_status']) ? $data['whatsup_status'] : '');
        $list = $this->getWhatsupStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getAdminWhatsupStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['admin_whatsup_status']) ? $data['admin_whatsup_status'] : '');
        $list = $this->getAdminWhatsupStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getAdminList()
    {
        $adminModel = new AdminModel();
        $admins = $adminModel->field('id,nickname')->select();

        return $admins;
    }

    public function Admin()
    {
        return $this->belongsTo('Admin', 'admin_id', 'id');
    }

    // public function getAdminIdAttr($value, $data)
    // {
    //     // 把 admin_id 换成 nickname 展示
    //     $adminModel = new AdminModel();
    //     $admins = $adminModel->field('id,nickname')->select();

    //     $arr = ['0' => ''];
    //     foreach ($admins as $key => $v) {
    //         $arr[$v['id']] = $v['nickname'];
    //     }

    //     return $arr[$value];
    // }

    public function getCountryList()
    {
        $countryModel = new CountryModel();
        $countrys = $countryModel->field('id,name')->select();

        $arr = [];
        foreach ($countrys as $key => $value) {
            $arr[$value['id']] = $value['name'];
        }

        return $arr;
    }

    // public function getCountryIdsAttr($value, $data)
    // {
    //     $countryModel = new CountryModel();
    //     $countrys = $countryModel->where('id', 'in', $value)->field('name')->select();

    //     $arr = [];
    //     foreach ($countrys as $key => $value) {
    //         array_push($arr, $value['name']);
    //     }
    //     return implode(',', $arr);
    // }

    public function Material()
    {
        return $this->belongsTo('Material', 'material_id', 'id');
    }

    // public function getMaterialIdAttr($value, $data)
    // {
    //     $materialModel = new MaterialModel();
    //     $material = $materialModel->where('id', $value)->field('name')->find();

    //     return $material['name'];
    // }
}
